//
//  BGOwnInviteBidLogsCell.m
//  bigMall
//
//  Created by LTOVE on 16/8/19.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGOwnInviteBidLogsCell.h"
#import "BGOwnInviteBidLogsModel.h"
#import "BGDateFormaTool.h"
#define lingMagin 5
static NSString *const reuseIdentify = @"cell";
@implementation BGOwnInviteBidLogsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setModel:(BGOwnInviteBidLogsModel *)model
{
    //添加所有子空间
    [self setUpAllSubViewsWith:model];
}

- (void)setUpAllSubViewsWith:(BGOwnInviteBidLogsModel *)model
{
        //内容视图
    UIView *contentView = [[UIView alloc]init];
    contentView.width = screenW;
    contentView.backgroundColor = lightwhite_;
    //cell分割线
    UIView *cellLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, BGMaginButtom)];
    cellLineView.backgroundColor = dockwhite_;
    [contentView addSubview:cellLineView];
    //标题
    NSString *paramStr = [NSString stringWithFormat:@"%@/%@",model.price,model.unit];
    NSString *titleStr = [NSString stringWithFormat:@"%@以%@成交",model.name,paramStr];
    NSDictionary *dic = @{
                          NSFontAttributeName:Font30,
                          NSForegroundColorAttributeName:textBlackColor
                          };
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:titleStr attributes:dic];
    NSRange range = [titleStr rangeOfString:@"以"];
    [attrStr addAttribute:NSForegroundColorAttributeName value:read_ range:NSMakeRange(range.location + 1, paramStr.length)];
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.attributedText = attrStr;
    [titleLabel sizeToFit];
    titleLabel.x = BGMaginLeft;
    titleLabel.y = CGRectGetMaxY(cellLineView.frame) + lingMagin;
    [contentView addSubview:titleLabel];
    //时间
    NSString *timeStr = [BGDateFormaTool dateFormateWithDateStr:model.add_time];
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.text = timeStr;
    timeLabel.textColor = textGrayColor;
    timeLabel.font = Font30;
    [timeLabel sizeToFit];
    timeLabel.centerY = titleLabel.centerY;
    timeLabel.x = screenW - BGMaginLeft - timeLabel.width;
    [contentView addSubview:timeLabel];
    //属性
//    NSDictionary *dic = @{
//                          NSFontAttributeName:Font30,
//                          NSForegroundColorAttributeName:textBlackColor
//                          };
    NSMutableAttributedString *attrStrs = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"投标%@%@%@",model.title,model.number,model.unit] attributes:dic];
    [attrStrs addAttribute:NSForegroundColorAttributeName value:read_ range:NSMakeRange(model.title.length + 2, model.number.length)];
    UILabel *detialLabel = [[UILabel alloc]init];
    detialLabel.attributedText = attrStrs;
    [detialLabel sizeToFit];
    detialLabel.x = titleLabel.x;
    detialLabel.y = lingMagin + CGRectGetMaxY(titleLabel.frame);
    [contentView addSubview:detialLabel];
    //分割线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(detialLabel.frame), screenW, 1)];
    lineView.backgroundColor = dockwhite_;
    lineView.alpha = 0.5;
    [contentView addSubview:lineView];
    //btn视图
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(lineView.frame), screenW, 33)];
//    btnView.backgroundColor = read_;
    [self addBtnWithSuperView:btnView title:model.result_str tag:model.tag];
    [contentView addSubview:btnView];
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat heigh = cellLineView.height + lingMagin + titleLabel.height + lingMagin + detialLabel.height +  lineView.height + btnView.height;
    contentView.height = heigh;
//    contentView.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:contentView];
}
/**
 *  添加btn
 *
 *  @param view  view description
 *  @param title title description
 */
- (void)addBtnWithSuperView:(UIView *)view title:(NSString *)title tag:(NSInteger)tag
{
    if ([title isEqualToString:@"投标中"]) {
        
//        两个按钮 等待确认
        // 结束按钮
        UIButton *resevebtn = [self btnCreatWithtitle:@"接受"];
        resevebtn.x = view.width - BGMaginLeft - resevebtn.width;
        resevebtn.centerY = view.height * 0.5;
        [resevebtn addTarget:self action:@selector(resovebtnClick:) forControlEvents:UIControlEventTouchUpInside];
        resevebtn.tag = tag;
        [view addSubview:resevebtn];
        //拒绝按钮
        UIButton *refuseBtn = [self btnCreatWithtitle:@"拒绝"];
        refuseBtn.x = resevebtn.x - lingMagin - refuseBtn.width;
        [refuseBtn addTarget:self action:@selector(refusebtnClick:) forControlEvents:UIControlEventTouchUpInside];
        refuseBtn.centerY = resevebtn.centerY;
        refuseBtn.tag = tag;
        [view addSubview:refuseBtn];
        
    }else if ([title isEqualToString:@"中标"]){
        //中标 也就是已接受
        UIButton *resevebtn = [self btnCreatWithtitle:@"已接受"];
        resevebtn.x = view.width - BGMaginLeft - resevebtn.width;
        resevebtn.centerY = view.height * 0.5;
        resevebtn.selected = YES;
        resevebtn.tag = tag;
        [view addSubview:resevebtn];
    }else{
     //没有中标  也就是被拒绝了
        UIButton *resevebtn = [self btnCreatWithtitle:@"已拒绝"];
        resevebtn.x = view.width - BGMaginLeft - resevebtn.width;
        resevebtn.centerY = view.height * 0.5;
        resevebtn.selected = YES;
        resevebtn.tag = tag;
        [view addSubview:resevebtn];
    }
}

- (UIButton *)btnCreatWithtitle:(NSString *)title
{
    UIButton *resevebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [resevebtn setTitle:title forState:UIControlStateNormal];
    [resevebtn setTitleColor:lightwhite_ forState:UIControlStateNormal];
    [resevebtn setSize:CGSizeMake(80, 30)];
    [resevebtn setBackgroundImage:[UIImage imageWithColor:read_ size:resevebtn.size] forState:UIControlStateNormal];
    [resevebtn setBackgroundImage:[UIImage imageWithColor:lightGray_ size:resevebtn.size] forState:UIControlStateSelected];
    [resevebtn setFont:Font28];
    resevebtn.layer.cornerRadius = 5;
    resevebtn.layer.masksToBounds = YES;
    return resevebtn;
}
/**
 *  点击了拒绝按钮
 */
- (void)refusebtnClick:(UIButton *)btn
{
    [self setDelegateWithBtn:btn];
}
/**
 *  点击了接受按钮
 */
- (void)resovebtnClick:(UIButton *)btn{
    [self setDelegateWithBtn:btn];
}

- (void)setDelegateWithBtn:(UIButton *)btn
{
    if ([_delegate respondsToSelector:@selector(bidLogBtnClick:)]) {
        [_delegate bidLogBtnClick:btn];
    }
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGOwnInviteBidLogsCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
